package com.itheima.sh.controller;

import com.itheima.sh.mapper.UserMapper;
import com.itheima.sh.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * @Description:
 * @Version: V1.0
 */
@RestController
@RequestMapping("user")
@Slf4j  // 日志注解
public class UserController {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    UserMapper userMapper;

    @Autowired
    RedisTemplate redisTemplate;


    @GetMapping("findAll")
    public List findAll() {
//        List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from tb_user");
//
//        for (Map<String, Object> map : maps) {
//            System.out.println(map);
//        }

        // String
        redisTemplate.opsForValue().set("hello", "world");

        // Hash  Map
        redisTemplate.opsForHash().put("key", "k1", "v1");
        redisTemplate.opsForHash().put("key", "k2", "v2");

        // set  无序不重复
        redisTemplate.opsForSet().add("setkey", 1, 1, 2, 3, 4, 5);

        // list 有序可重复
        redisTemplate.opsForList().leftPush("lkey", 1);
        redisTemplate.opsForList().leftPush("lkey", 1);
        redisTemplate.opsForList().leftPush("lkey", 2);
        redisTemplate.opsForList().leftPush("lkey", 3);
        redisTemplate.opsForList().leftPush("lkey", 4);

//        log.info();
//        log.error();
//        log.warn();
//        log.trace();

        // 取值
        Object value = redisTemplate.opsForValue().get("hello");
        System.out.println(value);

        Object k1 = redisTemplate.opsForHash().get("key", "k1");
        System.out.println(k1);

        Set setkey = redisTemplate.opsForSet().members("setkey");
        System.out.println(setkey);

        List lkey = redisTemplate.opsForList().range("lkey", 0, -1);
        System.out.println(lkey);


        //******************
        redisTemplate.boundValueOps("bk").set("v1111");

        List<User> userList = userMapper.findAll();
        return userList;
    }
}
